/**
 * Copyright (c) 2019, ShangHai YTO Co., Ltd.
 * All right reserved.
 *
 * THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF YTO
 * CO., LTD.  THE CONTENTS OF THIS FILE MAY NOT BE DISCLOSED
 * TO THIRD PARTIES, COPIED OR DUPLICATED IN ANY FORM, IN WHOLE OR IN PART,
 * WITHOUT THE PRIOR WRITTEN PERMISSION OF YTO
 * CO., LTD.
 */
package org.sc.xl.algorithm.easyexcel226;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.write.handler.AbstractRowWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import lombok.Data;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFColor;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @description: TODO 请在此添加描述
 * @reason: TODO ADD REASON(可选)
 * @author FXL
 *
 * @date 2024/10/19 12:51 
 * @since JDK 1.8
 */
public class StyleHandler extends AbstractRowWriteHandler {


    private List<String> sheetNameList;

    private List<StyleModel> cellStyleList = new ArrayList<>();


    public StyleHandler(List<StyleModel> cellStyleList) {
        if (CollectionUtil.isEmpty(cellStyleList)) {
            return;
        }
        cellStyleList = cellStyleList.stream().filter(x -> x != null
                //判断sheet名称KEY是否存在
                && StrUtil.isNotBlank(x.getSheetName())

                //判断背景颜色KEY是否存在
                && (x.getBackgroundColor() == null || x.getBackgroundColor() instanceof IndexedColors || x.getBackgroundColor() instanceof XSSFColor)).collect(Collectors.toList());
        this.cellStyleList = cellStyleList;
        sheetNameList = this.cellStyleList.stream().map(x -> x.getSheetName()).distinct().collect(Collectors.toList());
    }


    @Override
    public void afterRowDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Integer relativeRowIndex, Boolean isHead) {
        Sheet sheet = writeSheetHolder.getSheet();
        //不需要添加样式，或者当前sheet页不需要添加样式
        if (cellStyleList == null || cellStyleList.size() <= 0 || sheetNameList.contains(sheet.getSheetName()) == false) {
            return;
        }
        //获取当前行的样式信息
        List<StyleModel> rowCellStyleList = cellStyleList.stream().filter(x -> StrUtil.equals(x.getSheetName(), sheet.getSheetName()) && x.getRowIndex() == relativeRowIndex).collect(Collectors.toList());
        //该行不需要设置样式
        if (rowCellStyleList == null || rowCellStyleList.size() <= 0) {
            return;
        }
        for (StyleModel cellStyleModel : rowCellStyleList) {
            //设置单元格样式
            setCellStyle(cellStyleModel, row);
        }
        //删除已添加的样式信息
        cellStyleList.removeAll(rowCellStyleList);
        //重新获取要添加的sheet页姓名
        sheetNameList = cellStyleList.stream().map(x -> x.getSheetName()).distinct().collect(Collectors.toList());
    }

    private void setCellStyle(StyleModel cellStyleModel, Row row) {
        //背景颜色
        Object backgroundColor = cellStyleModel.getBackgroundColor();
        //列索引
        int colIndex = cellStyleModel.getColIndex();
        //边框样式
        Cell cell = row.getCell(colIndex);
        if (cell == null) {
            cell = row.createCell(colIndex);
        }
        XSSFCellStyle style = (XSSFCellStyle) cell.getRow().getSheet().getWorkbook().createCellStyle();
        // 克隆出一个 style
        style.cloneStyleFrom(cell.getCellStyle());
        //设置背景颜色
        if (backgroundColor != null) {
            //使用IndexedColors定义的颜色
            if (backgroundColor instanceof IndexedColors) {
                style.setFillForegroundColor(((IndexedColors) backgroundColor).getIndex());
            }
            //使用自定义的RGB颜色
            else if (backgroundColor instanceof XSSFColor) {
                style.setFillForegroundColor((XSSFColor) backgroundColor);
            }
            style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        }
        cell.setCellStyle(style);
    }


}

